﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;

using Microsoft.SqlServer.Management.Common;
using System.Collections;

namespace Quanly_dethi.Class
{
    class csC_ThiSinh
    {
        class csC_ThiSinh
        {
            private ketnoi kn;

            public ketnoi Kn
            {
                get { return kn; }
                set { kn = value; }
            }
            private string databasename;

            public string Databasename
            {
                get { return databasename; }
                set { databasename = value; }
            }
            public csC_ThiSinh()
            {
                Kn = new ketnoi();
                databasename = "";
            }
            public csC_ThiSinh(string servername, string user, string pass, string dbname)
            {
                Kn = new ketnoi(servername, user, pass);
                databasename = dbname;
            }
            public void Insert(csE_ThiSinh x)
            {
                Database db = Kn._Server.Databases[databasename];
                string s = "insert into ThiSinh(TenTS, Ngaysinh, Gioitinh, Malop) values(N'" + x.TenTS.ToString() + "','" + DateTime.Parse(x.Ngaysinh.ToString()) + "','" + int.Parse(x.Gioitinh.ToString()) + "',N'" + x.Malop.ToString() + "')";


                db.ExecuteWithResults(s);
            }

            public DataTable GetThiSinh(csE_ThiSinh u)
            {

                Database db = Kn._Server.Databases[databasename];
                string s = "select top 1 MaTS, TenSV, Ngaysinh, Gioitinh, Lop from ThiSinh order by MaTS DESC";
                DataTable k = db.ExecuteWithResults(s).Tables[0];
                //if (k.Rows.Count > 0)
                //{
                //    return true;
                //}
                return k; ;
            }

            public Boolean update(string duongdan)
            {

                Database db = Kn._Server.Databases[databasename];
                string s = "RESTORE DATABASE [QUANLY_DETHI] FROM  DISK =" + "'" + duongdan + "'" + " WITH  FILE = 1,  NOUNLOAD,  STATS = 10";
                DataTable k = db.ExecuteWithResults(s).Tables[0];
                if (k.Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }

            public void ThemKetQua(int MaTS, float Diem, int Made, int MaCH, int MaDA, DateTime ngay)
            {
                try
                {
                    Database db = Kn._Server.Databases[databasename];
                    string sql = "insert Table_KetQua values('" + MaTS + "','" + MaCH + "','" + Diem + "','" + MaDA + "','" + ngay + "')";
                    //db.ExecuteWithResults(t);
                }
                catch (Exception)
                {
                    throw new Exception("Không thể thêm");
                }
            }
        }  
    }
}
